#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "./uthash/include/uthash.h"

struct HashTable 
{
    int key, val;
    UT_hash_handle hh;
};



int maxSubAryLen(int* nums, int numSize, int k)
{
    int maxLen = 0;
    struct HashTable* cnt = NULL;
    int preSum = 0;
    for (int i = 0; i < numSize; ++i)
    {
        preSum += nums[i];
        struct HashTable* tmp = NULL;
        int need = preSum - k;
        HASH_FIND_INT(cnt, &need, tmp);
        if (preSum == k) {
            maxLen = i + 1;
        } else if (tmp != NULL) {
            maxLen = fmax(maxLen, i - tmp->val);
            printf("\t%d, %d, %d, %d\n", i, maxLen, need, tmp->val);
        } 
        HASH_FIND_INT(cnt, &preSum, tmp);
        if (tmp == NULL) {
            struct HashTable* newSum = (struct HashTable*)malloc(sizeof(struct HashTable));
            newSum->key = preSum, newSum->val = i;
            printf("%d, %d, %d\n", i, newSum->key, newSum->val);
            HASH_ADD_INT(cnt, key, newSum);
        }
    }

    return maxLen;
}



int main() 
{
    int nums[] = {-2, -1, 2, 1};
    int k = 1;
    int res = maxSubAryLen(nums, 4, k);
    printf("***%d", res);
    return 0;
}